package com.sikaryofficial.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeListReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityListReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityRecommendListReq;
import com.sikaryofficial.backend.domain.dto.resp.prize.PrizeDTO;
import com.sikaryofficial.backend.domain.dto.resp.productactivity.ActivityDTO;
import com.sikaryofficial.backend.domain.entity.Prize;
import com.sikaryofficial.backend.mapper.PrizeMapper;
import com.sikaryofficial.backend.service.IPrizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 奖品Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-11-02
 */
@Service
public class PrizeServiceImpl extends ServiceImpl<PrizeMapper, Prize> implements IPrizeService {
    @Autowired
    private PrizeMapper prizeMapper;

    @Override
    public Map<Long, Prize> getPrizeListByIds(List<Long> prizeIds) {
        if(CollUtil.isEmpty(prizeIds)){
            return Maps.newHashMap();
        }
        List<Prize> prizes = this.list(new LambdaQueryWrapper<Prize>()
                .in(Prize::getPrizeId, prizeIds)
                .eq(Prize::getDeletedVersion, 0L)
        );
        if(CollUtil.isEmpty(prizes)){
            return Maps.newHashMap();
        }
        return prizes.stream().collect(Collectors.toMap(Prize::getPrizeId, Function.identity(), (k1, k2) -> k1));
    }

    @Override
    public Prize getPrizeListById(Long prizeId) {
        if (Objects.isNull(prizeId)) {
            return null;
        }
        return this.getOne(new LambdaQueryWrapper<Prize>()
                .eq(Prize::getPrizeId, prizeId)
                .eq(Prize::getDeletedVersion, 0L)
        );
    }

    /**
     * 获取活动分页数据
     *
     * @param customerPage
     * @param req
     * @return
     */
    @Override
    public IPage<ActivityDTO> getBackendActivityList(Page<ActivityDTO> customerPage, ActivityListReq req) {
        return prizeMapper.getBackendActivityList(customerPage,req);
    }

    /**
     * 获取活动分页数据
     *
     * @return
     */
    @Override
    public IPage<ActivityDTO> getActivityByPage(Page<ActivityDTO> customerPage, ActivityRecommendListReq req) {
        return prizeMapper.getActivityByPage(customerPage, req);
    }

    /**
     * 获取活动详情
     *
     * @param activityId
     * @return
     */
    @Override
    public ActivityDTO getActivityDetail(Long activityId) {
        return prizeMapper.getActivityDetail(activityId);
    }

    /**
     * 根据租户ID 查询活动总数
     *
     * @param tenantId
     * @return
     */
    @Override
    public int getActivityCountByTenantId(Integer tenantId) {
        return prizeMapper.getActivityCountByTenantId(tenantId);
    }

    /**
     * 品牌专栏-活动列表查询
     *
     * @param customerPage
     * @param tenantId
     * @return
     */
    @Override
    public IPage<ActivityDTO> getActivityListByBrandSpecial(Page<ActivityDTO> customerPage, Integer tenantId) {
        return prizeMapper.getActivityListByBrandSpecial(customerPage, tenantId);
    }

    @Override
    public IPage<PrizeDTO> selectPrizeByPage(Page<PrizeDTO> customerPage, PrizeListReq req, List<Long> excludePrizeIds) {
        return prizeMapper.selectPrizeByPage(customerPage, req, excludePrizeIds);
    }
}
